package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<BaseCategory1> findAllCategory1() {
        return baseCategory1Mapper.selectList(null);

    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //      select id,name,is_top from base_category3 where is_top=1
        //            and category2_id in (select category2_id from base_category_view where category1_id=1);
        LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategory2::getCategory1Id, category1Id);

        List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(wrapper);
        //获取所有的category2Id
        List<Long> category2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());

        //获取所有的category3
        LambdaQueryWrapper<BaseCategory3> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(BaseCategory3::getCategory2Id, category2Ids).eq(BaseCategory3::getIsTop,1).last(" limit 7 ");
        List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(wrapper1);

        //返回数据
        return baseCategory3s;
    }

    /**
     * 根据一级分类id获取所有分类数据
     * @param category1Id
     * @return
     */
    @Override
    public JSONObject getCategory(Long category1Id) {
        //创建一级分类对象
        JSONObject c1=new JSONObject();
        BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
        c1.put("categoryId",category1Id);
        c1.put("categoryName",baseCategory1.getName());

        List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
        List<JSONObject> category2List = baseCategory2s.stream().map(baseCategory2 -> {
            //创建二级分类对象
            JSONObject c2 = new JSONObject();
            c2.put("categoryId", baseCategory2.getId());
            c2.put("categoryName", baseCategory2.getName());

            List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
            List<JSONObject> category3List = baseCategory3s.stream().map(baseCategory3 -> {
                //创建三级分类对象
                JSONObject c3 = new JSONObject();
                c3.put("categoryId", baseCategory3.getId());
                c3.put("categoryName", baseCategory3.getName());
                return c3;
            }).collect(Collectors.toList());
            c2.put("categoryChild", category3List);
            return c2;
        }).collect(Collectors.toList());
        c1.put("categoryChild", category2List);
        return c1;
    }

    /**
     * 根据分类3Id查询分类信息
     * @param category3Id
     * @return
     */
    @TsCache(prefix = "category:info:")
    @Override
    public BaseCategoryView findCategory(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    /**
     * 根据一级分类id获取标签属性值
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.selectBaseAttributeList(category1Id);
    }

    /**
     * 获取分类列表
     *
     * @return
     */
    @Override
    public List<JSONObject> getCategoryList() {
        /*//创建集合对象
        List<JSONObject> categoryList = new ArrayList<>();
        // 查询所有分类
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        //将所有的分类转换成map
        Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //使用迭代器将分类的map集合进行遍历
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            //获取迭代器中的数据
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            //获取到一级分类中的key值
            Long category1Id = entry.getKey();
            //获取一级分类中的List列表值
            List<BaseCategoryView> categoryViewList = entry.getValue();

            JSONObject category1 = new JSONObject();

            //将category的id封装
            category1.put("categoryId", category1Id);
            category1.put("categoryName", categoryViewList.get(0).getCategory1Name());


            //创建一个集合来存储二级分类
            ArrayList<JSONObject> categoryChildList2 = new ArrayList<>();

            //获取二级分类
            Map<Long, List<BaseCategoryView>> map1 = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
            //通过迭代器，遍历二级分类中的数据
            while (iterator1.hasNext()) {
                //获取到迭代器中的数据
                Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
                //获取到迭代器中的key，也就是category2ID
                Long category2Id = entry1.getKey();
                //获取到迭代器数据中的三级分类的数据集合
                List<BaseCategoryView> categoryViewList1 = entry1.getValue();
                //创建存放二级分类的对象
                JSONObject category2 = new JSONObject();

                //将
                category2.put("categoryId", category2Id);
                category2.put("categoryName", categoryViewList1.get(0).getCategory2Name());


                //创建一个集合来收集三级分类的数据
                ArrayList<JSONObject> categoryChildList3 = new ArrayList<>();
                //将二级分类中的数据集进行收集
                List<JSONObject> categoryList3 = categoryViewList1.stream().map(baseCategoryView -> {
                    //创建一个类存放三级分类对象
                    JSONObject category3 = new JSONObject();

                    //获取分类3中的k，v
                    Long categoryId = baseCategoryView.getCategory3Id();
                    String category3Name = baseCategoryView.getCategory3Name();
                    category3.put("categoryId", categoryId);
                    category3.put("categoryName", category3Name);
                    return category3;
                }).collect(Collectors.toList());
                category2.put("categoryChild",categoryList3);

                categoryChildList2.add(category2);
            }
            category1.put("categoryChild",categoryChildList2 );
            categoryList.add(category1);
        }
        return categoryList;
        */
        /*//1.
        //创建集合
        ArrayList<JSONObject> list = new ArrayList<>();
        //查询所有分类，用我们创建的视图进行查询 select * from
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        //将所有的分类转换成map并且根据一级分类的id进行分组
        Map<Long,List<BaseCategoryView>> category1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //根据迭代器进行遍历
        Iterator<Map.Entry<Long,List<BaseCategoryView>>> iterator = category1Map.entrySet().iterator();
        while(iterator.hasNext()){
            //创建一个对象来接收category1的内容
            JSONObject category1 = new JSONObject();
            //获取到迭代器中的数据
            Map.Entry<Long, List<BaseCategoryView>> map1 = iterator.next();
            //根据map1，获取到id和名字
            Long category1Id = map1.getKey();
            //获取到分类1 的名字
            List<BaseCategoryView> categoryViewList = map1.getValue();
            category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
            category1.put("categoryId",category1Id);

            //根据category1的数据列表获取迭代获取二级分类的数据
            //那就创建一个集合来存储二级分类的数据
            ArrayList<JSONObject> categoryList2 = new ArrayList<>();
            //将二级分类的数据转换成map
            Map<Long,List<BaseCategoryView>> category2Map = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //根据迭代器对二级分类的数据进行遍历
            Iterator<Map.Entry<Long,List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();
            while(iterator1.hasNext()){
                //创建存储二级分类数据的对象
                JSONObject category2 = new JSONObject();
                //获取迭代器的数据
                Map.Entry<Long, java.util.List<BaseCategoryView>> map2 = iterator1.next();
                //获取到二级分类
                List<BaseCategoryView> categoryViewList1 = map2.getValue();
                //获取到二级分类的id
                Long category2Id = map2.getKey();
                //
                category2.put("categoryId",category2Id);
                category2.put("categoryName",categoryViewList1.get(0).getCategory2Name());

                //获取到三级分类
                List<JSONObject> category3List = categoryViewList1.stream().map(baseCategoryView -> {
                    //创建一个存放三级分类的对象
                    JSONObject category3 = new JSONObject();
                    Long category3Id = baseCategoryView.getCategory3Id();
                    String category3Name = baseCategoryView.getCategory3Name();
                    category3.put("categoryId", category3Id);
                    category3.put("categoryName", category3Name);
                    return category3;
                }).collect(Collectors.toList());
                category2.put("categoryChild",category3List);
                categoryList2.add(category2);
            }
            category1.put("categoryChild",categoryList2);
            list.add(category1);
        }
        return list;*/
        //创建一个存放三级分类的集合
        List<JSONObject> list = new ArrayList<>();
        //根据视图查询所有列表的数据
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        //拿到所有的分类的数据，创建一个收集二级分类的集合
        Map<Long, List<BaseCategoryView>> map1 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map1.entrySet().iterator();
        while(iterator.hasNext()){
            //创建存放1的对象
            JSONObject category1 = new JSONObject();
            Map.Entry<Long, List<BaseCategoryView>> next1 = iterator.next();
            Long category1Id = next1.getKey();
            List<BaseCategoryView> baseCategoryViewList1 = next1.getValue();
            category1.put("categoryId",category1Id);
            category1.put("categoryName",baseCategoryViewList1.get(0).getCategory1Name());

            //创建一个存储二级分类的数组
            ArrayList<JSONObject> list2 = new ArrayList<>();
            //拿到二级分类的数据
            Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map2.entrySet().iterator();
            while (iterator1.hasNext()){
                //创建存放二级分类的对象
                JSONObject category2 = new JSONObject();
                //获取迭代器的数据
                Map.Entry<Long, List<BaseCategoryView>> next2 = iterator1.next();
                Long category2Id = next2.getKey();
                List<BaseCategoryView> baseCategoryViewList2 = next2.getValue();
                category2.put("categoryId",category2Id);
                category2.put("categoryName",baseCategoryViewList2.get(0).getCategory2Name());

                //
                List<JSONObject> list3 = baseCategoryViewList2.stream().map(baseCategoryView -> {
                    //创建存放三级分类的对象
                    JSONObject category3 = new JSONObject();
                    Long category3Id = baseCategoryView.getCategory3Id();
                    String category3Name = baseCategoryView.getCategory3Name();
                    category3.put("categoryId", category3Id);
                    category3.put("categoryName", category3Name);
                    return category3;
                }).collect(Collectors.toList());
                category2.put("categoryChild",list3);
                list2.add(category2);
            }
            category1.put("categoryChild",list2);
            list.add(category1);
        }
        return list;
    }
}
